summaryrefslogtreecommitdiff
path: root/app/api/data-room/[projectId]/download-folder/[folderId]/route.ts
blob: bba7066fb1b2d831d381410a2014decebab9d341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// app/api/data-room/[projectId]/download-folder/[folderId]/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { FileService, type FileAccessContext } from '@/lib/services/fileService';
import { promises as fs } from 'fs';
import path from 'path';
import archiver from 'archiver';
import db from "@/db/db";
import { fileItems } from "@/db/schema/fileSystem";
import { eq, and } from "drizzle-orm";

interface FileWithPath {
  file: any;
  absolutePath: string;
  relativePath: string;
}

export async function GET(
  request: NextRequest,
  { params }: { params: { projectId: string; folderId: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const context: FileAccessContext = {
      userId: Number(session.user.id),
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    // 폴더 정보 가져오기
    const folder = await db.query.fileItems.findFirst({
      where: and(
        eq(fileItems.id, params.folderId),
        eq(fileItems.projectId, params.projectId)
      ),
    });

    if (!folder || folder.type !== 'folder') {
      return NextResponse.json(
        { error: '폴더를 찾을 수 없습니다' },
        { status: 404 }
      );
    }

    const fileService = new FileService();
    const downloadableFiles: FileWithPath[] = [];
    const unauthorizedFiles: string[] = [];
    
    // 재귀적으로 폴더 내 모든 파일 가져오기 및 권한 확인
    const processFolder = async (
      folderId: string, 
      folderPath: string = ''
    ): Promise<void> => {
      const items = await db.query.fileItems.findMany({
        where: and(
          eq(fileItems.parentId, folderId),
          eq(fileItems.projectId, params.projectId)
        ),
      });

      for (const item of items) {
        if (item.type === 'file') {
          // 파일 권한 확인
          const hasAccess = await fileService.checkFileAccess(
            item.id,
            context,
            'download'
          );

          if (!hasAccess) {
            // 권한이 없는 파일 기록
            unauthorizedFiles.push(path.join(folderPath, item.name));
            continue;
          }

          if (!item.filePath) continue;

          // 실제 파일 경로 구성
          const nasPath = process.env.NAS_PATH || "/evcp_nas";
          const isProduction = process.env.NODE_ENV === "production";
          
          let absolutePath: string;
          if (isProduction) {
            const relativePath = item.filePath.replace('/api/files/', '');
            absolutePath = path.join(nasPath, relativePath);
          } else {
            absolutePath = path.join(process.cwd(), 'public', item.filePath);
          }

          // 파일 존재 여부 확인
          try {
            await fs.access(absolutePath);
            downloadableFiles.push({
              file: item,
              absolutePath,
              relativePath: path.join(folderPath, item.name)
            });
            
            // 다운로드 카운트 증가 및 로그 기록
            await fileService.downloadFile(item.id, context);
          } catch (error) {
            console.warn(`파일을 찾을 수 없습니다: ${absolutePath}`);
          }
        } else if (item.type === 'folder') {
          // 하위 폴더 재귀 처리
          await processFolder(
            item.id, 
            path.join(folderPath, item.name)
          );
        }
      }
    };

    // 폴더 처리 시작
    await processFolder(params.folderId, folder.name);

    // 권한이 없는 파일이 있으면 다운로드 차단
    if (unauthorizedFiles.length > 0) {
      return NextResponse.json(
        { 
          error: '일부 파일에 대한 다운로드 권한이 없습니다',
          unauthorizedFiles: unauthorizedFiles,
          unauthorizedCount: unauthorizedFiles.length,
          message: `다음 파일들에 대한 권한이 없어 폴더 다운로드가 취소되었습니다: ${unauthorizedFiles.slice(0, 5).join(', ')}${unauthorizedFiles.length > 5 ? ` 외 ${unauthorizedFiles.length - 5}개` : ''}`
        },
        { status: 403 }
      );
    }

    // 다운로드할 파일이 없는 경우
    if (downloadableFiles.length === 0) {
      return NextResponse.json(
        { error: '다운로드 가능한 파일이 없습니다' },
        { status: 404 }
      );
    }

    // 파일 크기 합계 체크 (최대 500MB)
    const totalSize = downloadableFiles.reduce((sum, item) => 
      sum + (item.file.size || 0), 0
    );
    
    const maxSize = 500 * 1024 * 1024; // 500MB
    if (totalSize > maxSize) {
      return NextResponse.json(
        { 
          error: `폴더 크기가 너무 큽니다 (${(totalSize / 1024 / 1024).toFixed(2)}MB). 최대 500MB까지 다운로드 가능합니다.`,
          totalSize: totalSize,
          maxSize: maxSize,
          fileCount: downloadableFiles.length
        },
        { status: 400 }
      );
    }

    console.log(`📦 폴더 다운로드 시작: ${folder.name} (${downloadableFiles.length}개 파일, ${(totalSize / 1024 / 1024).toFixed(2)}MB)`);

    // ZIP 스트림 생성
    const archive = archiver('zip', {
      zlib: { level: 5 } // 압축 레벨
    });

    // 스트림을 Response로 변환
    const stream = new ReadableStream({
      start(controller) {
        archive.on('data', (chunk) => controller.enqueue(chunk));
        archive.on('end', () => controller.close());
        archive.on('error', (err) => {
          console.error('Archive error:', err);
          controller.error(err);
        });
      },
    });

    // 파일들을 ZIP에 추가 (폴더 구조 유지)
    for (const { file, absolutePath, relativePath } of downloadableFiles) {
      try {
        const fileBuffer = await fs.readFile(absolutePath);
        archive.append(fileBuffer, { name: relativePath });
      } catch (error) {
        console.error(`파일 추가 실패: ${relativePath}`, error);
      }
    }

    // ZIP 완료
    archive.finalize();

    // Response Headers 설정
    const headers = new Headers();
    headers.set('Content-Type', 'application/zip');
    headers.set('Content-Disposition', `attachment; filename="${encodeURIComponent(folder.name)}.zip"`);
    headers.set('Cache-Control', 'no-cache, no-store, must-revalidate');
    headers.set('Pragma', 'no-cache');
    headers.set('Expires', '0');
    headers.set('X-File-Count', downloadableFiles.length.toString());
    headers.set('X-Total-Size', totalSize.toString());
    
    return new NextResponse(stream, {
      status: 200,
      headers,
    });

  } catch (error) {
    console.error('폴더 다운로드 오류:', error);
    return NextResponse.json(
      { error: '폴더 다운로드에 실패했습니다' },
      { status: 500 }
    );
  }
}

// 폴더 다운로드 전 권한 체크 (선택적)
export async function HEAD(
  request: NextRequest,
  { params }: { params: { projectId: string; folderId: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return new NextResponse(null, { status: 401 });
    }

    const context: FileAccessContext = {
      userId: Number(session.user.id),
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    const fileService = new FileService();
    let totalFiles = 0;
    let unauthorizedCount = 0;
    let totalSize = 0;

    // 재귀적으로 권한 체크
    const checkFolder = async (folderId: string): Promise<void> => {
      const items = await db.query.fileItems.findMany({
        where: and(
          eq(fileItems.parentId, folderId),
          eq(fileItems.projectId, params.projectId)
        ),
      });

      for (const item of items) {
        if (item.type === 'file') {
          totalFiles++;
          totalSize += item.size || 0;
          
          const hasAccess = await fileService.checkFileAccess(
            item.id,
            context,
            'download'
          );
          
          if (!hasAccess) {
            unauthorizedCount++;
          }
        } else if (item.type === 'folder') {
          await checkFolder(item.id);
        }
      }
    };

    await checkFolder(params.folderId);

    const headers = new Headers();
    headers.set('X-Total-Files', totalFiles.toString());
    headers.set('X-Unauthorized-Files', unauthorizedCount.toString());
    headers.set('X-Total-Size', totalSize.toString());
    headers.set('X-Can-Download', unauthorizedCount === 0 ? 'true' : 'false');
    
    return new NextResponse(null, {
      status: unauthorizedCount > 0 ? 403 : 200,
      headers,
    });

  } catch (error) {
    console.error('권한 체크 오류:', error);
    return new NextResponse(null, { status: 500 });
  }
}